<html>
    <head>
        <meta charset="utf-8">
        <title>使用Python将两张照片透明重叠</title>
        <link rel="stylesheet" href="../assets/stylesheets/global.css">
        <link rel="stylesheet" href="../assets/stylesheets/words.css">
        <link rel="stylesheet" href="../assets/stylesheets/monokai.css">
        <link rel="stylesheet" href="../assets/stylesheets/table.css">
        <link rel="shortcut icon" href="../assets/images/favicon.ico" type="image/x-icon">
        <link rel="icon" href="../assets/images/favicon.ico" type="image/x-icon">
        <script>
            // 统计代码
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

            ga('create', 'UA-93231524-1', 'auto');
            ga('send', 'pageview');
        </script>
    </head>
    <body>
        <div id="header">
            <a href="../index.html"><div id="logo">JG</div></a>
        </div>
        <div id="container" class="typo">
            <div id="article">
                <h1>使用Python将两张照片透明重叠</h1>
                <h4>Posted September 30, 2015</h4>

                <p>透明重叠最主要用的是<code>Image.blend</code>方法(详情请看第二个代码块), 第一个代码块主要是将多个图片剪切到一张大图, 然后用这张大图和另外一张非剪切的大图进行透明重叠， 主要<code>Image.blend</code>方法第三个参数是相对于第一张图片透明度。 </p>
<div class="code-wrapper"><span class="lang-label">Python</span><div class="highlight"><pre><span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd">File Name: merge.py</span>
<span class="sd">Author: JackeyGao</span>
<span class="sd">mail: junqi.gao@shuyun.com</span>
<span class="sd">Created Time: 一  6/ 1 13:27:49 2015</span>
<span class="sd">&#39;&#39;&#39;</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">Image</span>

<span class="k">def</span> <span class="nf">image_resize</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">)):</span>
    <span class="sd">&quot;&quot;&quot;调整图片大小</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">img</span><span class="o">.</span><span class="n">mode</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;L&#39;</span><span class="p">,</span> <span class="s1">&#39;RGB&#39;</span><span class="p">):</span>
            <span class="n">img</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="s1">&#39;RGB&#39;</span><span class="p">)</span>
        <span class="n">img</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="k">return</span> <span class="n">img</span>

<span class="k">def</span> <span class="nf">image_merge</span><span class="p">(</span><span class="n">images</span><span class="p">,</span> <span class="n">output_dir</span><span class="o">=</span><span class="s1">&#39;output&#39;</span><span class="p">,</span> <span class="n">output_name</span><span class="o">=</span><span class="s1">&#39;merge.jpg&#39;</span><span class="p">,</span> \
                <span class="n">restriction_max_width</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">restriction_max_height</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;垂直合并多张图片</span>
<span class="sd">    images - 要合并的图片路径列表</span>
<span class="sd">    ouput_dir - 输出路径</span>
<span class="sd">    output_name - 输出文件名</span>
<span class="sd">    restriction_max_width - 限制合并后的图片最大宽度，如果超过将等比缩小</span>
<span class="sd">    restriction_max_height - 限制合并后的图片最大高度，如果超过将等比缩小</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">x_number</span> <span class="o">=</span> <span class="mi">10</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">images</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">5</span> <span class="k">else</span> <span class="nb">len</span><span class="p">(</span><span class="n">images</span><span class="p">)</span>
    <span class="n">y_number</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">images</span><span class="p">)</span> <span class="o">/</span> <span class="n">x_number</span><span class="p">))</span>
    <span class="n">total_height</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="c1"># 计算合成后图片的宽度（以最宽的为准）和高度</span>
    <span class="k">for</span> <span class="n">img_path</span> <span class="ow">in</span> <span class="n">images</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">img_path</span><span class="p">):</span>
            <span class="n">img</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">img_path</span><span class="p">)</span>
            <span class="n">img</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="mi">712</span><span class="p">,</span> <span class="mi">960</span><span class="p">))</span>
            <span class="n">width</span><span class="p">,</span> <span class="n">height</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">size</span>

            <span class="n">max_width</span> <span class="o">=</span> <span class="n">width</span> <span class="o">*</span> <span class="n">x_number</span>
            <span class="n">total_height</span> <span class="o">=</span> <span class="n">height</span> <span class="o">*</span> <span class="n">y_number</span>

    <span class="c1"># 产生一张空白图</span>
    <span class="n">new_img</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s1">&#39;RGB&#39;</span><span class="p">,</span> <span class="p">(</span><span class="n">max_width</span><span class="p">,</span> <span class="n">total_height</span><span class="p">),</span> <span class="mi">255</span><span class="p">)</span>
    <span class="c1"># 合并</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">=</span> <span class="n">n</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">for</span> <span class="n">img_path</span> <span class="ow">in</span> <span class="n">images</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">img_path</span><span class="p">):</span>
            <span class="n">img</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">img_path</span><span class="p">)</span>
            <span class="n">img</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="mi">712</span><span class="p">,</span> <span class="mi">960</span><span class="p">))</span>
            <span class="n">width</span><span class="p">,</span> <span class="n">height</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">size</span>
            <span class="k">print</span> <span class="n">width</span><span class="p">,</span> <span class="n">height</span>
            <span class="n">new_img</span><span class="o">.</span><span class="n">paste</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">))</span>

            <span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="n">x_number</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">y</span> <span class="o">+=</span> <span class="n">height</span>
                <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">x</span> <span class="o">+=</span> <span class="n">width</span>



    <span class="k">if</span> <span class="n">restriction_max_width</span> <span class="ow">and</span> <span class="n">max_width</span> <span class="o">&gt;=</span> <span class="n">restriction_max_width</span><span class="p">:</span>
        <span class="c1"># 如果宽带超过限制</span>
        <span class="c1"># 等比例缩小</span>
        <span class="n">ratio</span> <span class="o">=</span> <span class="n">restriction_max_height</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">max_width</span><span class="p">)</span>
        <span class="n">max_width</span> <span class="o">=</span> <span class="n">restriction_max_width</span>
        <span class="n">total_height</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">total_height</span> <span class="o">*</span> <span class="n">ratio</span><span class="p">)</span>
        <span class="n">new_img</span> <span class="o">=</span> <span class="n">image_resize</span><span class="p">(</span><span class="n">new_img</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="p">(</span><span class="n">max_width</span><span class="p">,</span> <span class="n">total_height</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">restriction_max_height</span> <span class="ow">and</span> <span class="n">total_height</span> <span class="o">&gt;=</span> <span class="n">restriction_max_height</span><span class="p">:</span>
        <span class="c1"># 如果高度超过限制</span>
        <span class="c1"># 等比例缩小</span>
        <span class="n">ratio</span> <span class="o">=</span> <span class="n">restriction_max_height</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">total_height</span><span class="p">)</span>
        <span class="n">max_width</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">max_width</span> <span class="o">*</span> <span class="n">ratio</span><span class="p">)</span>
        <span class="n">total_height</span> <span class="o">=</span> <span class="n">restriction_max_height</span>
        <span class="n">new_img</span> <span class="o">=</span> <span class="n">image_resize</span><span class="p">(</span><span class="n">new_img</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="p">(</span><span class="n">max_width</span><span class="p">,</span> <span class="n">total_height</span><span class="p">))</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">output_dir</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">output_dir</span><span class="p">)</span>
    <span class="n">save_path</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">output_dir</span><span class="p">,</span> <span class="n">output_name</span><span class="p">)</span>
    <span class="n">new_img</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">save_path</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">save_path</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">os</span>
    <span class="n">image_files</span> <span class="o">=</span> <span class="p">[</span> <span class="s2">&quot;input/</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&quot;input/&quot;</span><span class="p">)</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;pg&quot;</span><span class="p">)</span> <span class="p">]</span>

    <span class="n">image_files</span> <span class="o">=</span> <span class="n">image_files</span> <span class="o">*</span> <span class="mi">4</span>

    <span class="n">image_merge</span><span class="p">(</span><span class="n">images</span><span class="o">=</span><span class="n">image_files</span><span class="p">)</span>
</pre></div>
</div>
<p>将两张背景图重叠</p>
<div class="code-wrapper"><span class="lang-label">Python</span><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">Image</span>
<span class="n">img</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;bg.jpeg&quot;</span><span class="p">)</span>
<span class="n">img2</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;merge.jpg&quot;</span><span class="p">)</span>
<span class="n">merge</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">blend</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">img2</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">)</span>
<span class="n">merge</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s2">&quot;my.jpg&quot;</span><span class="p">)</span>
</pre></div>
</div>
            </div>
            <div id="footer">
                <a href="../words.html"><div id="more-words">MORE WORDS</div></a>
            </div>
        </div>
    </body>
</html>